/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class SzseAnnouncementJson extends BaseJsonAdapter{

    async doRequest(page){

        var options = {
            headers:{
              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
              'Host': 'www.szse.cn',
            },
            // uri:'http://www.bjp2p.com.cn/supervise/queryNews4List',
            uri:'http://www.szse.cn/api/disc/announcement/detailinfo?pageSize=30&pageNum=' + page + '&plateCode=szse',
            method:"GET",
            encoding:null
        }
        let body = await request(options)
        return body;
    }


    async reqJson(){
        let page = 1;
        let go = 1;
        let contents = [];
        let url = 'http://www.szse.cn/disclosure/listed/bulletinDetail/index.html?'
        do {
          let body = await this.doRequest(page);
          let decodeBody = iconv.decode(body, this.encoding).toString();
          let data = JSON.parse(decodeBody);
          if ( data && data.data) {
            page = page + 1;
            for(let row of data.data) {
              for(let info of row.announList) {
                contents.push({
                  title: info['title'],
                  href: url + info['id']
                });
              }
            }
          } else {
            go = 0;
          }

        } while (go > 0);

        return {data:contents}
    }
}
